package com.tgy.algorithm.base.other;

import java.util.Arrays;

public class _奇数放在奇数位置偶数放在偶数位置 {


    public static void oddEven(int[] nums) {
        if (nums == null || nums.length == 0 || nums.length == 1) {
            return;
        }
        int len = nums.length;
        int oddIndex = 1;
        int evenIndex = 0;
        int lastIndex = len - 1;
        while (oddIndex < len && evenIndex < len) {
            if (nums[lastIndex] % 2 == 0) {
                swap(nums,evenIndex,lastIndex);
                evenIndex += 2;
            }else {
                swap(nums,oddIndex,lastIndex);
                oddIndex += 2;
            }
        }
    }

    public static void swap(int[] nums,int one, int two) {
        int tmp = nums[one];
        nums[one] = nums[two];
        nums[two] = tmp;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3,2,1,2,3,1};
        oddEven(nums);
        System.out.println(Arrays.toString(nums));
    }
}
